//
//  SDHomeViewController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/29.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDHomeViewController.h"
#import "SDHomeHeaderView.h"
#import "SDHomeCell.h"
#import "SDHomeReusableView.h"
#import "SDHomeReusableView2.h"
#import "SDHomeProductData.h"
#import "SDHomeProductList.h"
#import "SDHomeProductModel.h"
#import "SDProductListVC.h"
#import "SDProductDetailVC.h"
#import "SDHomeViewModel.h"

static NSString *const cellIden = @"SDHomeCell";
static NSString *const ReusableViewIden = @"SDHomeReusableView";
static NSString *const ReusableViewIden2 = @"SDHomeReusableView2";
@interface SDHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)SDHomeViewModel *showModel;

@end

@implementation SDHomeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    self.navHeight.constant = SDNavBar_Height;
    self.bottom.constant = 49;
    
    [self.collectionView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellWithReuseIdentifier:cellIden];
    [self.collectionView registerNib:[UINib nibWithNibName:ReusableViewIden bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ReusableViewIden];
    
    [self.collectionView registerNib:[UINib nibWithNibName:ReusableViewIden2 bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ReusableViewIden2];
    [self loadData];

}

/**
 加载数据
 */
- (void)loadData
{
    [SDHomeViewModel clear];
    [[SDHomeViewModel initModel] loadDataWithBlock:^(BOOL success) {
        if (success) {
            self.showModel = [SDHomeViewModel initModel];
            [self.collectionView reloadData];
        }
    }];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.showModel.dataArray.count - 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SDHomeProductData *data = self.showModel.dataArray[section + 1];
    return data.list.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden forIndexPath:indexPath];
    SDHomeProductData *data = self.showModel.dataArray[indexPath.section + 1];
     SDHomeProductList *list = data.list[indexPath.item];
     [cell setModel:list];
     cell.kingCapImage.hidden = ![data.typeName isEqualToString:King];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 12;
    
    if (self.showModel.heightsArray.count > 0) {
        NSArray *hArray = self.showModel.heightsArray[indexPath.section];
        NSNumber *ber = hArray[indexPath.row/2];
         height = ber.floatValue;
    }
   height = height > 32? 32:15;
    
    return CGSizeMake((SDMainScreenWidth - 45)/2.0, 25 + 85*((SDMainScreenWidth - 45)/2.0/165.00) + height);
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.00;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SDHomeProductData *data2 = self.showModel.dataArray[indexPath.section + 1];
    if (kind == UICollectionElementKindSectionHeader) {
        
        if (indexPath.section == 0) {
            
            SDHomeReusableView2 *headerView2 = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ReusableViewIden2 forIndexPath:indexPath];
            SDHomeProductData *data1 = self.showModel.dataArray.firstObject;
            
            [headerView2.catoaryLab setText:data2.typeName];
            headerView2.listArray = data1.list;
            [headerView2.goBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
                [self goProductListVCWithProductData:data2];
            }];
            
            return headerView2;
            
        }else{
        SDHomeReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ReusableViewIden forIndexPath:indexPath];
            [headerView.titleLab setText:data2.typeName];
            [headerView.goBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
                [self goProductListVCWithProductData:data2];
            }];
        return headerView;
        }
    }
    
    return nil;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGFloat height = section == 0? 188:59;
    return CGSizeMake(SDMainScreenWidth, height);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDHomeProductData *data = self.showModel.dataArray[indexPath.section + 1];
    SDHomeProductList *list = data.list[indexPath.row];
    SDProductDetailVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDProductDetailVC class])];
    vc.list = list.bImg;
    [self.tabBarController.navigationController pushViewController:vc animated:YES];
    
}

/**
 进入商品列表页

 @param data 商品分类数据
 */
- (void)goProductListVCWithProductData:(SDHomeProductData *)data
{
    SDProductListVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDProductListVC class])];
    vc.type = data.id;
    vc.coateTitle = data.typeName;
    [self.tabBarController.navigationController pushViewController:vc animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
